import WebsocketClass from "@/utils/websocket"
import httpUrl from "@/utils/config"
export default {
  namespaced: true,
  state: {
    response: null,
    socket: null
  },
  mutations: {
    INIT: (state) => {
      let token = localStorage.getItem("chatToken")
      let wsUrl = httpUrl.socketUrl + token
      state.socket = new WebsocketClass(wsUrl, (data) => {
        state.response = data
      })
      state.socket.connect()
    },
    SEND: (state, data) => {
      const params = {
        type: 1,
        token: localStorage.getItem("chatToken"),
        responseBody: data
      }
      state.socket.send(params)
    }
  },
  actions: {
    init({commit}){
      commit("INIT")
    },
    send({commit, dispatch}, data) {
      commit("SEND", data)
      const now = new Date()
      dispatch("message/pushPrivateMessage", {
        ...data,
        conversationId: data.msgConversationId,
        createTime: now.getTime(),
        loading: true
      }, {root: true})
    }
  }
}